package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.TransitInfoService;
import com.suyuan.vo.NewTransitBatchVO;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.vo.TransitInfoVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2018/2/5.
 */
@Service(value = "transitInfoService")
public class TransitInfoServiceImpl implements TransitInfoService {
    @Resource
    private TransitInfoMapper transitInfoMapper;
    //@Resource
    //private TransitbatchInfoMapper batchMapper;
    @Resource
    private NewtransitbatchMapper newtransitbatchMapper;
    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;
    @Resource
    private TransportrouteInforMapper routeMapper;
    @Resource
    private CompanyInfoMapper companyMapper;

    public PageInfo list(Integer batchId, String compName , Page page) {
        if(StringUtils.isBlank(compName)){
            compName = null;
        }
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<TransitInfo> transitInfos = transitInfoMapper.likeInquiry(batchId,compName);
        PageInfo info = new PageInfo(transitInfos);
        List<TransitInfoVO> vos = new ArrayList<TransitInfoVO>();

        if(transitInfos.size() <= 0){
            return null;
        }
        for (TransitInfo transitInfo:(List<TransitInfo>)info.getList()) {
            //批次信息
            Newtransitbatch batch = newtransitbatchMapper.selectByPrimaryKey(transitInfo.getTransitBatchId());
            //公司信息
            CompanyInfo company = companyMapper.selectByPrimaryKey(batch.getCompid());
            //线路信息
            TransportRouteInfor route = routeMapper.selectByPrimaryKey(batch.getRouteid());
            //将批次信息、线路信息添加进VO类中
            NewTransitBatchVO batchVO = new NewTransitBatchVO(batch,route,company,null);
            //生猪信息
            PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(transitInfo.getAnimalId());
            vos.add(new TransitInfoVO(transitInfo,batchVO,purchaseInfo));
        }
        info.setList(vos);
        return info;
    }

    public int delete(Integer id) {
        return transitInfoMapper.deleteByPrimaryKey(id);
    }

    public int save(TransitInfo transitInfo) {
        return transitInfoMapper.insert(transitInfo);
    }

    public List<TransitInfoVO> inquiry(Integer batchId) {
        TransitInfoExample example = new TransitInfoExample();
        TransitInfoExample.Criteria criteria = example.createCriteria();
        criteria.andTransitbatchidEqualTo(batchId);
        //查出该物流批次的所有物流信息
        List<TransitInfo> transitInfos = transitInfoMapper.selectByExample(example);

        if(transitInfos.size() > 0){
            List<TransitInfoVO> vos = new ArrayList<TransitInfoVO>();
            for (TransitInfo transitInfo:transitInfos) {
                Newtransitbatch batch = newtransitbatchMapper.selectByPrimaryKey(transitInfo.getTransitBatchId());
                //线路信息
                TransportRouteInfor route = routeMapper.selectByPrimaryKey(batch.getRouteid());
                //公司信息
                CompanyInfo company = companyMapper.selectByPrimaryKey(batch.getCompid());
                //将批次信息、线路信息添加进VO类中
                NewTransitBatchVO batchVO = new NewTransitBatchVO(batch,route,company,null);
                PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(transitInfo.getAnimalId());
                vos.add(new TransitInfoVO(transitInfo,batchVO,purchaseInfo));
            }
            return vos;
        }
        return null;
    }

    public TransitInfo findById(Integer id) {
        return transitInfoMapper.selectByPrimaryKey(id);
    }

    public int update(TransitInfo transit) {
        return transitInfoMapper.updateByPrimaryKey(transit);
    }

    public List<PurchaseInfo> getPigs() {
        return purchaseInfoMapper.selectByExample(new PurchaseInfoExample());
    }
}
